package com.mystnihon.models;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import ca.benow.transmission.TransmissionClient;
import ca.benow.transmission.model.TorrentStatus;
import ca.benow.transmission.model.TorrentStatus.TorrentField;

public class Seedbox implements Serializable {

	private static final int DEFAULT_PORT = 9091;
	private String name;
	private String host;
	private int port;
	private int ftpPort;
	private String login;
	private String password;

	transient private TransmissionClient client;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * @return the login
	 */
	public String getLogin() {
		return login;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	public int getFtpPort() {
		return ftpPort;
	}

	public void setFtpPort(int ftpPort) {
		this.ftpPort = ftpPort;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param host
	 *            the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * @param port
	 *            the port to set
	 */
	public void setPort(int port) {
		if (port <= 0) {
			this.port = DEFAULT_PORT;
		} else {
			this.port = port;
		}
	}

	/**
	 * @param login
	 *            the login to set
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	public boolean connect() {
		if (validIdentification()) {
			client = new TransmissionClient(host, port, login, password);
		}
		if (client != null) {
			return true;
		}
		return false;
	}

	public TransmissionClient getClient() {

		return client;
	}

	public List<TorrentStatus> getAllTorrents(TorrentField[] fields)
			throws IOException {
		if (client != null) {
			List<TorrentStatus> list = client.getAllTorrents(fields);
			return list;
		} else {
			return null;
		}
	}

	private boolean validIdentification() {
		if (!(host == null || login == null || password == null)) {
			if (!(host.isEmpty() || login.isEmpty() || password.isEmpty())) {
				return true;
			}
		}

		return false;
	}

}
